package psl;

import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Mar 2, 2007
 * Time: 3:20:56 PM
 */
public class ROHTMLServices
{
    private static StringBuffer out;
    private static ResultSet moResultSet;

    public static String getCityList (ResultSet foResultSet)
    {
        out = new StringBuffer ();
        moResultSet = foResultSet;
        try
        {
            while (moResultSet.next ())
            {
                out.append ("<option value=" + moResultSet.getString ("city_name") + ">" + moResultSet.getString ("city_name") + "</option>");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
        return out.toString ();
    }

    public static String getCategoryListItem (ResultSet foResultSet, String fsOnClick)
    {
        out = new StringBuffer ();
        moResultSet = foResultSet;
        try
        {
            out.append ("<table border=0 cellpadding=0 cellspacing=0><tr>\r\n");
/*
            out.append ("<th class='thwide' align=center>CATEGORY</th>");
*/
            while (moResultSet.next ())
            {
                String msCategory = moResultSet.getString ("cat_name");
                out.append ("<td>\r\n");
                out.append ("<a class =list href=javascript:" + fsOnClick + "('" + msCategory + "')>" + msCategory + "</a>\r\n");
                out.append ("</td>\r\n");
            }
            out.append ("</tr></table>\r\n");
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
        return out.toString ();

    }

    public static String getCategoryList (ResultSet foResultSet, String fsOnClick)
    {
        out = new StringBuffer ();
        moResultSet = foResultSet;
        try
        {
            out.append ("<table border=0 cellpadding=0 cellspacing=0><tr>\r\n");
/*
            out.append ("<th class='thwide' align=center>CATEGORY</th>");
*/
            while (moResultSet.next ())
            {
                String msCategory = moResultSet.getString ("cat_name");
                out.append ("<td>\r\n");
                out.append ("<a class =list href=javascript:" + fsOnClick + "('" + msCategory + "')>" + msCategory + "</a>\r\n");
                out.append ("</td>\r\n");
            }
            out.append ("</tr></table>\r\n");
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
        return out.toString ();
    }

    
    public static String getUserProductDeatil (BasePage ROpage)
    {
        out = new StringBuffer ();
        try
        {
            out.append ("<table class=registere cellpadding=0 cellspacing=2 border=0 width=100%>\r\n" +
                    "<th class='thwide'>INDEX</th><th class='thwide'>order_no</th><th class='thwide' >NAME</th><th class='thwide'>COMAPY</th>\r\n" +
                    "<th class='thwide'>PRICE</th><th class='thwide'>QUANTITY</th ><th class='thwide'>TOTAL</th >" +
                    "<th class='thwide' align='center'>Order Date</th><th class='thwide' align='center'>Status</th>\r\n" +
                    "<th class='thwide' align='center'>Select</th>");
            char tag = 34;
            String function = tag + "setEnabled('continue',true);" + tag;
            int start = ROpage.miStartRow;
            System.err.println ("ROpage.miEndRow: " + ROpage.miEndRow);
            for (int i = start; i < ROpage.miEndRow; i++)
            {
                String lscolor = (start % 2 == 0) ? "#f7e5d2" : "#d2decd";
                out.append ("<tr onclick=" + function + " height=18 bgcolor=" + lscolor + ">");
                out.append ("<td align=center>" + (start + 1) + "</td>");
                for (int j = 0; j < ROpage.maRows.length; j++)
                {
                    if (j == (ROpage.maRows.length - 1))
                    {
                        out.append ("<td align=center><input type='checkbox' name=pid value='" + ROpage.maRows[j][i] + "'></td>\r\n");
                    }
                    else
                    {
                        out.append ("<td align=center>" + ROpage.maRows[j][i] + "</td>");
                    }
                }
                out.append ("</tr>");
                if (start != 0)
                {
                    if ((start + 1) % 10 == 0)
                    {
                        ROpage.miStartRow = start + 1;
                        break;
                    }
                }
                start++;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
        return out.toString ();
    }

    public static String getItemBillList (UserSession foUserSession, String ROType, BasePage ROPage)
    {
        out = new StringBuffer ();
        try
        {
            if (ROType.equalsIgnoreCase ("SaleEntryBill"))
            {
                out.append ("<table border=0 width=100% >\r\n" +
                        "<th class='thwide'>CATEGORY</th ><th class='thwide' >NAME</th><th class='thwide'>COMAPY</th>\r\n" +
                        "<th class='thwide'>PRICE</th><th class='thwide'>QUANTITY</th ><th class='thwide'>TOTAL</th >\r\n");
            }
            else
            {
                char tag = 34;
                String function = tag + "setSelected('RemoveID');" + tag;

                out.append ("<table border=0 width=100% >\r\n" +
                        "<th class='thwide'>CATEGORY</th ><th class='thwide' >NAME</th><th class='thwide'>COMAPY</th>\r\n" +
                        "<th class='thwide'>PRICE</th><th class='thwide'>QUANTITY</th ><th class='thwide'>TOTAL</th >" +
                        "<th class='thwide' onclick=" + function + " align='center'>REMOVE ALL</th>\r\n");
            }
            int count = 0, index = 1;
            float lfTotal= 0.0f;
            int lsQuantity = 1;
            char tag = 34;
            String function = tag + "setEnabled('update',true);" + tag;
            String[] laColumn = { "Product_Id", "Product_Name", "Cat_Name", "Company", "Product_Price", "Qty_On_Hand", "Re_ord_level", "img" };
            int start = ROPage.miStartRow;
            System.err.println ("ROpage.miStartRow: " + ROPage.miStartRow);

            for (int row = start; row < ROPage.maRows[0].length; row++)
            {
                if (!foUserSession.mvSelsected.contains (ROPage.maRows[0][row]))
                {
                    System.err.println ("NOT CONTAIN:  "+ROPage.maRows[0][row]);
                }
                else
                {
                    if (foUserSession.mvQuantity != null && foUserSession.mvQuantity .size ()!=0 )
                    {
                        lsQuantity = (int) Float.parseFloat ((String)foUserSession.mvQuantity.get (ROPage.maRows[0][row]));
                        lfTotal += (Float.parseFloat(ROPage.maRows[4][row]) * lsQuantity);
                    }
                    if (ROType.equalsIgnoreCase ("SaleEntryBill"))
                    {
                        String lsPrice = (Float.parseFloat (ROPage.maRows[4][row]) * lsQuantity) + "";
                        String lscolor = (count% 2 == 0) ? "#f7e5d2" : "#d2decd";
                        out.append ("<tr height=20 id='row_no_" + count + "' bgcolor=" + lscolor + ">");
                        out.append ("<td align='left'>" + ROPage.maRows[2][row] + "</td>\r\n");
                        out.append ("<td align='left'>" + ROPage.maRows[1][row] + "</td>\r\n");
                        out.append ("<td align='left'>" + ROPage.maRows[3][row] + "</td>\r\n");
                        out.append ("<td align='left'>" + ROPage.maRows[4][row] + "$</td>\r\n");
                        out.append ("<td align='center'  width=10%>" + lsQuantity + "</td>\r\n");
                        out.append ("<td align='left'>" + lsPrice + "$</td>\r\n");
                        out.append ("<input type='hidden' name=productPrice value=" + lsPrice + ">\r\n");
                        out.append ("</tr>\r\n");
                        count++;
                    }
                    else
                    {
                        String lscolor = (count% 2 == 0) ? "#f7e5d2" : "#d2decd";
                        String function1 = tag + "displayImage('" + ROPage.maRows[7][row] + "', '" + count + "');" + tag;
                        String function2 = tag + "hideImage('" + ROPage.maRows[7][row] + "', '" + count + "');" + tag;
                        out.append ("<tr height=20 id='row_no_" + count + "' bgcolor=" + lscolor + " onmouseover=" + function1 + " onmouseout=" + function2 + ">");
                        out.append ("<td align='center'>" + ROPage.maRows[2][row] + "</td>\r\n");
                        out.append ("<td align='center'>" + ROPage.maRows[1][row] + "</td>\r\n");
                        out.append ("<td align='center'>" + ROPage.maRows[3][row] + "</td>\r\n");
                        out.append ("<td align='left'>" + ROPage.maRows[4][row] + "</td>\r\n");
                        out.append ("<input type='hidden' name=quantity value=" + count + ":" + ROPage.maRows[5][row] + "" + ">\r\n");
                        out.append ("<td align='left'><input onclick=" + function + " type='text' size=3 name=quantityDis value='" + lsQuantity + "" + "'></td>\r\n");
                        out.append ("<td align='left'>" + (Float.parseFloat (ROPage.maRows[4][row]) * lsQuantity) + "" + "$</td>\r\n");
                        out.append ("<td align='center'><input type='checkbox' onclick=" + function + " name=RemoveID " +
                                "value=" + count + ":" + ROPage.maRows[0][row] + ">\r\n");
                        out.append ("<input type='hidden' name=row_" + start + "  value=" + start + ">\r\n");

                        out.append ("<input type='hidden' name=proid value="+ ROPage.maRows[0][row] + ">\r\n");

                        out.append ("</tr>\r\n");
                        if (count != 0)
                        {
                            if ((count + 1) % 5 == 0)
                            {
                                ROPage.miStartRow = count + 1;
                                break;
                            }
                        }

                        count++;
                    }
                }
                start++;
            }
            foUserSession.msTotal = lfTotal+ "";
            out.append ("<tr height=35>" +
                    "<td align='left' colspan=5 bgcolor=#ccccff><b>Total</b></td>" +
                    "<td align='left' bgcolor=#ccccff colspan=2 ><b>" + lfTotal + "$</b></td>" +
                    "</tr>\r\n");
            out.append ("</table>\r\n");
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
        return out.toString ();
    }

    public static String getItemList (BasePage ROpage, String fsCategory)
    {
        out = new StringBuffer ();
        try
        {
            out.append ("<table border=0 width=100%>\r\n" +
                    "<th class='thwide'>SELECT</th >" +
                    "<th class='thwide'>NAME</th><th class='thwide'>COMAPY</th>\r\n" +
                    "<th class='thwide'>PRICE</th><th class='thwide' >CATEGORY</th>\r\n");
            char tag = 34;
            String function = tag + "setEnabled('addtocart',true);" + tag;
            int start = ROpage.miStartRow;
            System.err.println ("ROpage.miStartRow: " + ROpage.miStartRow);
            System.err.println ("ROpage.miEndRow: " + ROpage.miEndRow);
            int dataCount = 0;
            for (int row = start; row < ROpage.maRows[0].length/*ROpage.miEndRow*/; row++)
            {
                if (ROpage.maRows[2][row].equals (fsCategory))
                {
                    String lscolor = (dataCount % 2 == 0) ? "#f7e5d2" : "#d2decd";
                    String function1 = tag + "displayImage('" + ROpage.maRows[7][row] + "', '" + dataCount + "');" + tag;
                    String function2 = tag + "hideImage('" + ROpage.maRows[7][row] + "', '" + dataCount + "');" + tag;
                    out.append ("<tr height=20 id='row_no_" + dataCount + "' bgcolor=" + lscolor + " onmouseover=" + function1 + " onmouseout=" + function2 + ">");
                    out.append ("<td width='10%' align=center>" +
                            "<input type='checkbox' onclick=" + function + " name=ProId value='" + ROpage.maRows[0][row] + "'>" +
                            "</td>\r\n");
                    out.append ("<td align=left>" + ROpage.maRows[1][row] + "</td>");
                    out.append ("<td align=left>" + ROpage.maRows[3][row] + "</td>");
                    out.append ("<td align=left>" + ROpage.maRows[4][row] + "</td>");
                    out.append ("<td align=left>" + ROpage.maRows[2][row] + "</td>");
                    out.append ("<input type='hidden' name=row_" + dataCount + "  value=" + start + ">\r\n");
                    out.append ("</tr>");
                    if (dataCount != 0)
                    {
                        if ((dataCount + 1) % 5 == 0)
                        {
                            ROpage.miStartRow = start + 1;
                            break;
                        }
                    }
                    dataCount++;
                }
                start++;
            }
            out.append ("</tr></table>\r\n");
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
        return out.toString ();
    }
}
                                                                                    